package com.cc.police.controller;


import com.cc.police.config.LoginRequired;
import com.cc.police.entity.PPolice;
import com.cc.police.service.PPoliceService;
import com.cc.police.util.JsonResult;
import com.cc.police.util.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@CrossOrigin//前后端请求跨域注解
public class PPoliceController extends BaseController{
    @Autowired
    PPoliceService service;
    @Autowired
    RedisUtil redisUtil;


    /*查询当前正在：报警4、已确认3、屏蔽2、封锁1等信息*/
    @RequestMapping("/getPPoliceByConfirm")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> getPPoliceByConfirm(String confirm) throws ParseException {
        System.out.println("cnfirm= "+confirm);
//        JsonResult result = new JsonResult();
        List list = new ArrayList();
        List<PPolice> data = service.getPPoliceByConfirm(confirm);

        for (PPolice pPolice:data) {

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(pPolice.getTime());
            System.out.println(dateString);
            pPolice.setTimes(dateString);
            list.add(pPolice);
        }

        return new JsonResult<>(SUCCESS,list);
    }

    //正在报警修改为已确认报警3、封锁2、屏蔽1
    @RequestMapping("/updateConfirmThreeById")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<Void> updateConfirmThreeById(Integer id,String confirm,String variable) {
        JsonResult result = new JsonResult();
        Jedis jedis = redisUtil.getJedis();
        System.err.println("修改值"+confirm);
        System.err.println("变量名"+variable);
        try {
            //如果为3则是确认报警   删除redis锁  再次有问题还报警  其他封锁屏蔽不删除不会继续报警
            if(confirm.equals("3")){
                jedis.del("pol:" + variable + ":lock");
            }
            service.updateConfirmThreeById(id,confirm);
            return new JsonResult<>(SUCCESS);
        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试。");
        }
        return result;


    }



    /*搜索功能----车间*/
    @RequestMapping("/selectByGrups")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> selectByGrups(String groups) {

        System.out.println("groups=="+groups);

        List<PPolice> data =  service.selectByGrups(groups);

        return new JsonResult<>(SUCCESS,data);
    }



    /*搜索功能----报警级别紧急1重要2一般3*/
    @RequestMapping("/selectByLevels")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> selectByLevels(String levels) {

        System.out.println("selectByLevels=="+levels);

        List<PPolice> data =  service.selectByLevels(levels);

        return new JsonResult<>(SUCCESS,data);
    }

    /*搜索功能----根据时间查询*/
    @RequestMapping("/selectByTime")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> selectByTime(@Param("starttime") String starttime,
                                              @Param("endtime") String endtime) {

        System.out.println("selectByTime=="+starttime);
        System.out.println("selectByTime=="+endtime);

        List<PPolice> data =  service.selectByTime(starttime,endtime);

        return new JsonResult<>(SUCCESS,data);
    }





    /*图表功能----天组查询*/
    @RequestMapping("/selectChartByTime")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> selectChartByTime(@Param("starttime") String starttime,
                                                  @Param("groups") String groups) {

        System.err.println("时间字符串长度"+starttime.length());
        System.err.println("selectChartByTime=="+starttime);
//        System.out.println("selectChartByTime=="+endtime);
        JsonResult result = new JsonResult();

        try {

            if (starttime.length()>=8){
                String endtime = starttime+" 23:59:59";
                List<PPolice> data =  service.selectChartByTime(starttime,endtime,groups);
                return new JsonResult<>(SUCCESS,data.size());
            }else {
                String start = starttime+"-00 00:00:00";
                String endtime = starttime+"-31 23:59:59";
                List<PPolice> data =  service.selectChartByTime(start,endtime,groups);
                return new JsonResult<>(SUCCESS,data.size());
            }


        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }


        return result;
    }




    /*图表功能----组查询回显*/
    @RequestMapping("/selectGroups")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> selectByGroups() {
        PPolice pPolice = new PPolice();
        JsonResult result = new JsonResult();
        List list = new ArrayList();
        try {
            List<PPolice> data =  service.selectByGroups();
            for (PPolice groupsss:data) {
                list.add(groupsss);
            }
            pPolice.setGroups(" ");
            list.add(pPolice);
            return new JsonResult<>(SUCCESS,list);

        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }

        return result;
    }



    /*按小时划分查询某天数据*/
    @RequestMapping("/selectCountByHours")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> selectCountByHours(@Param("starttime") String starttime,@Param("groups") String groups
                                                       ) {

//        Map map = new HashMap();
//        map.put("00",0);
//        map.put("01",0);
//        map.put("02",0);
//        map.put("03",0);
//        map.put("04",0);
//        map.put("05",0);
//        map.put("06",0);
//        map.put("07",0);
//        map.put("08",0);
//        map.put("09",0);
//        map.put("10",0);
//        map.put("11",0);
//        map.put("12",0);
//        map.put("13",0);
//        map.put("14",0);
//        map.put("15",0);
//        map.put("16",0);
//        map.put("17",0);
//        map.put("18",0);
//        map.put("19",0);
//        map.put("20",0);
//        map.put("21",0);
//        map.put("22",0);
//        map.put("23",0);


        JsonResult result = new JsonResult();
        List list = new ArrayList();
        for (int i = 0; i <24 ; i++) {
            list.add(0);
        }
        System.out.println(list.size());
        System.out.println(list);

        try {
                String endtime = starttime+" 23:59:59";
                starttime = starttime+" 00:00:00";

                List<PPolice> data =  service.selectCountByHours(starttime,endtime,groups);



            for (PPolice pp:data) {
//                map.put(pp.getTimeaa(),pp.getCount());
//                System.err.println(pp.getTimeaa());
//                System.err.println(pp.getCount());

                Integer aa = Integer.valueOf(pp.getTimeaa());
                System.out.println(aa);
                list.set(aa,pp.getCount());

            }
            System.err.println("数据集合"+list);
               result.setState(SUCCESS);
//                result.setMap(map);
                result.setData(list);

        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }


        return result;
    }




    /*按天划分查询某天数据*/
    @RequestMapping("/selectCountByDays")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> selectCountByDays(@Param("starttime") String starttime,@Param("groups") String groups
    ) {

//        Map map = new HashMap();
//        map.put("01",0);
//        map.put("02",0);
//        map.put("03",0);
//        map.put("04",0);
//        map.put("05",0);
//        map.put("06",0);
//        map.put("07",0);
//        map.put("08",0);
//        map.put("09",0);
//        map.put("10",0);
//        map.put("11",0);
//        map.put("12",0);
//        map.put("13",0);
//        map.put("14",0);
//        map.put("15",0);
//        map.put("16",0);
//        map.put("17",0);
//        map.put("18",0);
//        map.put("19",0);
//        map.put("20",0);
//        map.put("21",0);
//        map.put("22",0);
//        map.put("23",0);
//        map.put("24",0);
//        map.put("25",0);
//        map.put("26",0);
//        map.put("27",0);
//        map.put("28",0);
//        map.put("29",0);
//        map.put("30",0);
//        map.put("31",0);



        JsonResult result = new JsonResult();
        List list = new ArrayList();
        for (int i = 0; i <31 ; i++) {
            list.add(0);
        }
        System.out.println(list.size());
        System.out.println(list);
        try {
            String endtime = starttime+"-31 23:59:59";
            starttime = starttime+"-01 00:00:00";

            List<PPolice> data =  service.selectCountByDays(starttime,endtime,groups);



            for (PPolice pp:data) {
//                map.put(pp.getTimeaa(),pp.getCount());

                Integer aa = Integer.valueOf(pp.getTimeaa()-1);
                System.out.println(aa);
                list.set(aa,pp.getCount());
            }

            result.setState(SUCCESS);
//            result.setMap(map);
            result.setData(list);

        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }


        return result;
    }





    /*图表功能----弹窗显示天数*/
    @RequestMapping("/showDays")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> showDays(Integer num,String groups) {
        PPolice pPolice = new PPolice();
        JsonResult result = new JsonResult();
        try {

            long nowtime  = new Date().getTime();
            System.out.println(nowtime);

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String endtime = formatter.format(nowtime);
            System.out.println(endtime);

            long aa = nowtime-num*86400000;
            SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
            String starttime = formatter1.format(aa);
            System.out.println(starttime);


                List<PPolice> data =  service.showDays(starttime,endtime,groups);
                return new JsonResult<>(SUCCESS,data);



        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }

        return result;
    }




    /*图表功能----饼状图数据报警条数查询*/
    @RequestMapping("/selectRingByTime")
    @ResponseBody
    @LoginRequired(loginSuccess = true)
    public JsonResult<List<PPolice>> selectRingByTime(@Param("starttime") String starttime
                                                       ) {

        System.err.println("时间字符串长度"+starttime.length());
        System.err.println("selectChartByTime=="+starttime);
//        System.out.println("selectChartByTime=="+endtime);
        JsonResult result = new JsonResult();
        List list = new ArrayList();
        Map map = new HashMap();
        Map map1 = new HashMap();
        try {

            if (starttime.length()>=8){
                String endtime = starttime+" 23:59:59";
                List<PPolice> data =  service.selectRingByTime(starttime,endtime);
//                for (PPolice p:data) {
//                    System.err.println(p);
//                }


                String aa = starttime.substring(0,7);
                String start = aa+"-00 00:00:00";
                String endtime1 = aa+"-31 23:59:59";
                List<PPolice> data1 =  service.selectRingByTime(start,endtime1);
                map.put("name","当天全部报警量");
                map.put("data",data.size());
                list.add(map);

                map1.put("name","当月全部报警量");
                map1.put("data",data1.size());
                list.add(map1);

                result.setState(SUCCESS);

                result.setData(list);

            }else {
                result.setMessage("日期数据格式长度不对，请重新尝试");

            }


        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }


        return result;
    }




}
